﻿/***************************************************************
* File    : XML Controller.cs
* Author  : Matthew Clifton
*
* Description:
*   Built to retrieve and store sensor ranges in an XML file
*
* History :
***************************************************************/

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;

namespace RLGDashboard
{
    public class XMLController
    {

        public void serializeToXML(RangeContainer container)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(RangeContainer));
            TextWriter textWriter = new StreamWriter("ranges.xml");
            serializer.Serialize(textWriter, container);
            textWriter.Close();
        }

        public RangeContainer retrieveFromXml()
        {
            RangeContainer ranges = null;
            XmlSerializer deserializer = new XmlSerializer(typeof(RangeContainer));
            if(File.Exists("ranges.xml"))
            {
                TextReader textReader = new StreamReader("ranges.xml");
                ranges = (RangeContainer)deserializer.Deserialize(textReader);
                textReader.Close();
            }
            else
            {
                ranges = new RangeContainer();
            }

            return ranges;
        }
    }

}
